/**
 *FileViewTableModel.java
 *@anthor Daniel Liu
 *2006-4-28 1:33:12
 **/
package lex.explorer.file;

import java.io.File;
import java.util.Locale;
import javax.swing.UIManager;
import javax.swing.table.AbstractTableModel;

import org.lex.explorer.FileInfo;

public class FileTableModel extends AbstractTableModel {
	private static final String[] COLUMN_NAMES = getLocalizedColumnNames();
	private static final FileInfo[] EMPTY = new FileInfo[0];
	private FileInfo[] data = EMPTY;

	// ��ʾ�ļ�ʱ�䡢�ļ����ļ�ͼ��ȵĸ�ʽ
	public FileTableModel() {
		super();
	}

	public void setContent(FileInfo[] files) {
		this.data = files;
		this.fireTableDataChanged();
	}

	public File getFile(int row) {
		return data[row].getFile();
	}

	public FileInfo getFileInfo(int row) {
		return data[row];
	}

	// ======================
	public int getRowCount() {
		return this.data.length;
	}

	public int getColumnCount() {
		return COLUMN_NAMES.length;
	}

	public String getColumnName(int column) {
		return COLUMN_NAMES[column];
	}

	public Object getValueAt(int row, int column) {
		FileInfo info = null;
		if (row >= data.length)
			info = FileInfo.FAKE;
		info = data[row];
		if (null == info)
			info = FileInfo.FAKE;
		switch (column) {
		case 0:
			// name & icon
			return info;
		case 1:
			// size
			return info;
		case 2:
			// type
			return info.getType();
		case 3:
			// date
			return info.getDate();
		case 4:
			// attribute
			return info.getAttribute();
		default:
			return FileInfo.FAKE;
		}
	}

	// Localized
	private static String[] getLocalizedColumnNames() {
		Locale locale = Locale.getDefault();
		String fileNameHeaderText = UIManager.getString("FileChooser.fileNameHeaderText", locale);
		String fileSizeHeaderText = UIManager.getString("FileChooser.fileSizeHeaderText", locale);
		String fileTypeHeaderText = UIManager.getString("FileChooser.fileTypeHeaderText", locale);
		String fileDateHeaderText = UIManager.getString("FileChooser.fileDateHeaderText", locale);
		String fileAttrHeaderText = UIManager.getString("FileChooser.fileAttrHeaderText", locale);
		return new String[] { fileNameHeaderText, fileSizeHeaderText, fileTypeHeaderText, fileDateHeaderText, fileAttrHeaderText };
	}
}
